package com.github.thinwonton.showcase.springboot.rabbitmq.config;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.http.client.Client;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.core.RabbitManagementTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * RabbitMQ配置,禁用了springboot对rmq的自动配置，因为只需配置RMQ的客户端操作模板
 *
 * @author hugo
 * @date 2018/7/24
 */
@Configuration
@ConfigurationProperties(prefix = "rabbitmq")
public class RabbitMQConfig {

    /**
     * Login user to authenticate to the broker.
     */
    private String username;

    /**
     * Login to authenticate against the broker.
     */
    private String password;

    /**
     * Comma-separated list of addresses to which the client should connect.
     */
    private String addresses;

    /**
     * 操作管理rabbitmq API的模板
     *
     * @return
     */
    @Bean
    public RabbitManagementTemplate rabbitManagementTemplate() {
//        Client rabbitClient;
//        try {
//            rabbitClient = new Client(
//                    "http://localhost:15672/api/",
//                    "guest2",
//                    "guest"
//            );
//        }
//        catch (Exception e) {
//            throw new AmqpException(e);
//        }
//
//        return new RabbitManagementTemplate(
//                "http://localhost:15672/api/",
//                "guest",
//                "guest");
        //初始化RabbitManagementTemplate
        return new RabbitManagementTemplate(this.addresses, this.username, this.password);
//        return new RabbitManagementTemplate("http://guest:guest@127.0.0.1:15672/api/");
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAddresses(String addresses) {
        this.addresses = addresses;
    }
}
